﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework.Controls;
using TinyERP.Framework;
using TinyERP.Core.Business;
using TinyERP.Core.DTO;
using System.Collections;
using Bamboo.Core.Common.Collection;
using TinyERP.Framework.Controls;
using log4net;
using TinyERP.Core.View.Controls;

namespace TinyERP.Core.View
{
    public partial class FunctionManagementForm : BaseForm
    {
        private const int ALLOW_VIEW_COL = 1;
        private const int ALLOW_CREATE_COL = 2;
        private const int ALLOW_EDIT_COL = 3;
        private const int ALLOW_DELETE_COL = 4;




        private ILog logger = LogManager.GetLogger(typeof(FunctionManagementForm));

        private SortableBindingList<GroupACLDTO> groupACLSortableList = null;
        private UINodeDTO selectedUINode = null;
        private SaveButton saveButton;
        private UpButton upButton;
        private DownButton downButton;
        
        public IUINodeService UINodeService
        {
            get
            {
                return (IUINodeService)ServiceLocator.GetService("UINodeServiceProxy");
            }
        }

        public IGroupACLService GroupACLService
        {
            get
            {
                return (IGroupACLService)ServiceLocator.GetService("GroupACLServiceProxy");
            }
        }
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Component Designer generated code

        private ComponentFactory.Krypton.Toolkit.KryptonPanel panelMain;
        private ComponentFactory.Krypton.Toolkit.KryptonSplitContainer splitterHorizontal;
        private KHeaderGroup headerGroupFunctionTree;
        private KHeaderGroup headerGroupFunctionDetail;
        private ComponentFactory.Krypton.Toolkit.KryptonSplitContainer splitterDetail;
        private ComponentFactory.Krypton.Toolkit.KryptonPanel panelDetailInfo;
        private KLabel lblAssignment;
        private KCheckBox chkEnable;
        private KLabel lblDescription;
        private KTextBox txtModule;
        private KLabel lblModule;
        private KTextBox txtName;
        private KLabel lblName;
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel;
        private KTextBox txtDescription;
        private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
        private System.Windows.Forms.DataGridViewTextBoxColumn colGroupName;
        private System.Windows.Forms.DataGridViewCheckBoxColumn colAllowView;
        private System.Windows.Forms.DataGridViewCheckBoxColumn colAllowCreate;
        private System.Windows.Forms.DataGridViewCheckBoxColumn colAllowEdit;
        private System.Windows.Forms.DataGridViewCheckBoxColumn colAllowDelete;
        private TinyERP.Framework.Controls.CustomDataGridView gridRoleAccessibility;
        private System.Windows.Forms.TreeView tvFunction;
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FunctionManagementForm));
            this.panelMain = new ComponentFactory.Krypton.Toolkit.KryptonPanel();
            this.splitterHorizontal = new ComponentFactory.Krypton.Toolkit.KryptonSplitContainer();
            this.headerGroupFunctionTree = new TinyERP.Framework.Controls.KHeaderGroup();
            this.headerGroupFunctionDetail = new TinyERP.Framework.Controls.KHeaderGroup();
            this.splitterDetail = new ComponentFactory.Krypton.Toolkit.KryptonSplitContainer();
            this.panelDetailInfo = new ComponentFactory.Krypton.Toolkit.KryptonPanel();
            this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.saveButton = new TinyERP.Framework.Controls.SaveButton();
            this.upButton = new TinyERP.Framework.Controls.UpButton();
            this.downButton = new TinyERP.Framework.Controls.DownButton();
            this.txtName = new TinyERP.Framework.Controls.KTextBox();
            this.lblModule = new TinyERP.Framework.Controls.KLabel();
            this.txtModule = new TinyERP.Framework.Controls.KTextBox();
            this.lblName = new TinyERP.Framework.Controls.KLabel();
            this.txtDescription = new TinyERP.Framework.Controls.KTextBox();
            this.chkEnable = new TinyERP.Framework.Controls.KCheckBox();
            this.lblDescription = new TinyERP.Framework.Controls.KLabel();
            this.gridRoleAccessibility = new TinyERP.Framework.Controls.CustomDataGridView();
            this.colGroupName = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colAllowView = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.colAllowCreate = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.colAllowEdit = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.colAllowDelete = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.lblAssignment = new TinyERP.Framework.Controls.KLabel();
            tvFunction = new System.Windows.Forms.TreeView();
            ((System.ComponentModel.ISupportInitialize)(this.panelMain)).BeginInit();
            this.panelMain.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitterHorizontal)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.splitterHorizontal.Panel1)).BeginInit();
            this.splitterHorizontal.Panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitterHorizontal.Panel2)).BeginInit();
            this.splitterHorizontal.Panel2.SuspendLayout();
            this.splitterHorizontal.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.headerGroupFunctionTree)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.headerGroupFunctionTree.Panel)).BeginInit();
            this.headerGroupFunctionTree.Panel.SuspendLayout();
            this.headerGroupFunctionTree.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.headerGroupFunctionDetail)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.headerGroupFunctionDetail.Panel)).BeginInit();
            this.headerGroupFunctionDetail.Panel.SuspendLayout();
            this.headerGroupFunctionDetail.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitterDetail)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.splitterDetail.Panel1)).BeginInit();
            this.splitterDetail.Panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitterDetail.Panel2)).BeginInit();
            this.splitterDetail.Panel2.SuspendLayout();
            this.splitterDetail.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.panelDetailInfo)).BeginInit();
            this.panelDetailInfo.SuspendLayout();
            this.tableLayoutPanel.SuspendLayout();
            this.flowLayoutPanel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.gridRoleAccessibility)).BeginInit();
            this.SuspendLayout();
            // 
            // tvFunction
            // 
            tvFunction.Dock = System.Windows.Forms.DockStyle.Fill;
            tvFunction.FullRowSelect = true;
            tvFunction.HideSelection = false;
            tvFunction.Location = new System.Drawing.Point(0, 0);
            tvFunction.Name = "tvFunction";
            tvFunction.ShowNodeToolTips = true;
            tvFunction.Size = new System.Drawing.Size(216, 478);
            tvFunction.TabIndex = 0;
            // 
            // panelMain
            // 
            this.panelMain.Controls.Add(this.splitterHorizontal);
            this.panelMain.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelMain.Location = new System.Drawing.Point(0, 0);
            this.panelMain.Name = "panelMain";
            this.panelMain.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.panelMain.PanelBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.PanelClient;
            this.panelMain.Size = new System.Drawing.Size(810, 522);
            this.panelMain.TabIndex = 0;
            // 
            // splitterHorizontal
            // 
            this.splitterHorizontal.ContainerBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.PanelClient;
            this.splitterHorizontal.Cursor = System.Windows.Forms.Cursors.Default;
            this.splitterHorizontal.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitterHorizontal.Location = new System.Drawing.Point(0, 0);
            this.splitterHorizontal.Name = "splitterHorizontal";
            this.splitterHorizontal.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            // 
            // splitterHorizontal.Panel1
            // 
            this.splitterHorizontal.Panel1.Controls.Add(this.headerGroupFunctionTree);
            this.splitterHorizontal.Panel1.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.splitterHorizontal.Panel1.PanelBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.PanelClient;
            // 
            // splitterHorizontal.Panel2
            // 
            this.splitterHorizontal.Panel2.Controls.Add(this.headerGroupFunctionDetail);
            this.splitterHorizontal.Panel2.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.splitterHorizontal.Panel2.PanelBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.PanelClient;
            this.splitterHorizontal.SeparatorStyle = ComponentFactory.Krypton.Toolkit.SeparatorStyle.LowProfile;
            this.splitterHorizontal.Size = new System.Drawing.Size(810, 522);
            this.splitterHorizontal.SplitterDistance = 218;
            this.splitterHorizontal.TabIndex = 0;
            // 
            // headerGroupFunctionTree
            // 
            this.headerGroupFunctionTree.CollapseTarget = ComponentFactory.Krypton.Toolkit.HeaderGroupCollapsedTarget.CollapsedToPrimary;
            this.headerGroupFunctionTree.Dock = System.Windows.Forms.DockStyle.Fill;
            this.headerGroupFunctionTree.GroupBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.ButtonNavigatorStack;
            this.headerGroupFunctionTree.GroupBorderStyle = ComponentFactory.Krypton.Toolkit.PaletteBorderStyle.ControlClient;
            this.headerGroupFunctionTree.HeaderStylePrimary = ComponentFactory.Krypton.Toolkit.HeaderStyle.Secondary;
            this.headerGroupFunctionTree.HeaderStyleSecondary = ComponentFactory.Krypton.Toolkit.HeaderStyle.Secondary;
            this.headerGroupFunctionTree.Location = new System.Drawing.Point(0, 0);
            this.headerGroupFunctionTree.Name = "headerGroupFunctionTree";
            this.headerGroupFunctionTree.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            // 
            // headerGroupFunctionTree.Panel
            // 
            this.headerGroupFunctionTree.Panel.Controls.Add(tvFunction);
            this.headerGroupFunctionTree.Size = new System.Drawing.Size(218, 522);
            this.headerGroupFunctionTree.TabIndex = 0;
            this.headerGroupFunctionTree.Text = "Function menu";
            this.headerGroupFunctionTree.ValuesPrimary.Description = "";
            this.headerGroupFunctionTree.ValuesPrimary.Heading = "Function menu";
            this.headerGroupFunctionTree.ValuesPrimary.Image = null;
            this.headerGroupFunctionTree.ValuesSecondary.Description = "";
            this.headerGroupFunctionTree.ValuesSecondary.Heading = "Description";
            this.headerGroupFunctionTree.ValuesSecondary.Image = null;
            // 
            // headerGroupFunctionDetail
            // 
            this.headerGroupFunctionDetail.CollapseTarget = ComponentFactory.Krypton.Toolkit.HeaderGroupCollapsedTarget.CollapsedToPrimary;
            this.headerGroupFunctionDetail.Dock = System.Windows.Forms.DockStyle.Fill;
            this.headerGroupFunctionDetail.GroupBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.ControlClient;
            this.headerGroupFunctionDetail.GroupBorderStyle = ComponentFactory.Krypton.Toolkit.PaletteBorderStyle.ControlClient;
            this.headerGroupFunctionDetail.HeaderStylePrimary = ComponentFactory.Krypton.Toolkit.HeaderStyle.Secondary;
            this.headerGroupFunctionDetail.HeaderStyleSecondary = ComponentFactory.Krypton.Toolkit.HeaderStyle.Secondary;
            this.headerGroupFunctionDetail.Location = new System.Drawing.Point(0, 0);
            this.headerGroupFunctionDetail.Name = "headerGroupFunctionDetail";
            this.headerGroupFunctionDetail.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            // 
            // headerGroupFunctionDetail.Panel
            // 
            this.headerGroupFunctionDetail.Panel.Controls.Add(this.splitterDetail);
            this.headerGroupFunctionDetail.Size = new System.Drawing.Size(587, 522);
            this.headerGroupFunctionDetail.TabIndex = 0;
            this.headerGroupFunctionDetail.Text = "Detail information";
            this.headerGroupFunctionDetail.ValuesPrimary.Description = "";
            this.headerGroupFunctionDetail.ValuesPrimary.Heading = "Detail information";
            this.headerGroupFunctionDetail.ValuesPrimary.Image = null;
            this.headerGroupFunctionDetail.ValuesSecondary.Description = "";
            this.headerGroupFunctionDetail.ValuesSecondary.Heading = "";
            this.headerGroupFunctionDetail.ValuesSecondary.Image = null;
            // 
            // splitterDetail
            // 
            this.splitterDetail.ContainerBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.PanelClient;
            this.splitterDetail.Cursor = System.Windows.Forms.Cursors.Default;
            this.splitterDetail.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitterDetail.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
            this.splitterDetail.IsSplitterFixed = true;
            this.splitterDetail.Location = new System.Drawing.Point(0, 0);
            this.splitterDetail.Name = "splitterDetail";
            this.splitterDetail.Orientation = System.Windows.Forms.Orientation.Horizontal;
            this.splitterDetail.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            // 
            // splitterDetail.Panel1
            // 
            this.splitterDetail.Panel1.Controls.Add(this.panelDetailInfo);
            this.splitterDetail.Panel1.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.splitterDetail.Panel1.PanelBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.ControlClient;
            this.splitterDetail.Panel1MinSize = 210;
            // 
            // splitterDetail.Panel2
            // 
            this.splitterDetail.Panel2.Controls.Add(this.gridRoleAccessibility);
            this.splitterDetail.Panel2.Controls.Add(this.lblAssignment);
            this.splitterDetail.Panel2.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.splitterDetail.Panel2.PanelBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.HeaderSecondary;
            this.splitterDetail.Panel2MinSize = 100;
            this.splitterDetail.SeparatorStyle = ComponentFactory.Krypton.Toolkit.SeparatorStyle.LowProfile;
            this.splitterDetail.Size = new System.Drawing.Size(585, 496);
            this.splitterDetail.SplitterDistance = 210;
            this.splitterDetail.TabIndex = 0;
            // 
            // panelDetailInfo
            // 
            this.panelDetailInfo.Controls.Add(this.tableLayoutPanel);
            this.panelDetailInfo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelDetailInfo.Location = new System.Drawing.Point(0, 0);
            this.panelDetailInfo.Name = "panelDetailInfo";
            this.panelDetailInfo.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.panelDetailInfo.PanelBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.TabOneNote;
            this.panelDetailInfo.Size = new System.Drawing.Size(585, 210);
            this.panelDetailInfo.TabIndex = 14;
            // 
            // tableLayoutPanel
            // 
            this.tableLayoutPanel.ColumnCount = 3;
            this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 117F));
            this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 373F));
            this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 97F));
            this.tableLayoutPanel.Controls.Add(this.flowLayoutPanel1, 0, 3);
            this.tableLayoutPanel.Controls.Add(this.txtName, 1, 0);
            this.tableLayoutPanel.Controls.Add(this.lblModule, 0, 1);
            this.tableLayoutPanel.Controls.Add(this.txtModule, 1, 1);
            this.tableLayoutPanel.Controls.Add(this.lblName, 0, 0);
            this.tableLayoutPanel.Controls.Add(this.txtDescription, 1, 2);
            this.tableLayoutPanel.Controls.Add(this.chkEnable, 2, 0);
            this.tableLayoutPanel.Controls.Add(this.lblDescription, 0, 2);
            this.tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel.Name = "tableLayoutPanel";
            this.tableLayoutPanel.RowCount = 4;
            this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 28F));
            this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 28F));
            this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 92F));
            this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tableLayoutPanel.Size = new System.Drawing.Size(585, 210);
            this.tableLayoutPanel.TabIndex = 23;
            // 
            // flowLayoutPanel1
            // 
            this.tableLayoutPanel.SetColumnSpan(this.flowLayoutPanel1, 2);
            this.flowLayoutPanel1.Controls.Add(this.saveButton);
            this.flowLayoutPanel1.Controls.Add(this.upButton);
            this.flowLayoutPanel1.Controls.Add(this.downButton);
            this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.flowLayoutPanel1.Location = new System.Drawing.Point(3, 151);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.flowLayoutPanel1.Size = new System.Drawing.Size(484, 56);
            this.flowLayoutPanel1.TabIndex = 25;
            // 
            // saveButton
            // 
            this.saveButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.saveButton.ButtonStyle = ComponentFactory.Krypton.Toolkit.ButtonStyle.Standalone;
            this.saveButton.Image = ((System.Drawing.Image)(resources.GetObject("saveButton.Image")));
            this.saveButton.Location = new System.Drawing.Point(3, 3);
            this.saveButton.Name = "saveButton";
            this.saveButton.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.saveButton.ResourceKey = "Save";
            this.saveButton.ResourceModuleKey = null;
            this.saveButton.Size = new System.Drawing.Size(99, 25);
            this.saveButton.TabIndex = 0;
            this.saveButton.Text = "Save";
            this.saveButton.Values.ExtraText = "Save";
            this.saveButton.Values.Image = ((System.Drawing.Image)(resources.GetObject("saveButton.Values.Image")));
            this.saveButton.Values.ImageStates.ImageCheckedNormal = null;
            this.saveButton.Values.ImageStates.ImageCheckedPressed = null;
            this.saveButton.Values.ImageStates.ImageCheckedTracking = null;
            this.saveButton.Values.Text = "";
            // 
            // upButton
            // 
            this.upButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.upButton.ButtonStyle = ComponentFactory.Krypton.Toolkit.ButtonStyle.Standalone;
            this.upButton.Image = ((System.Drawing.Image)(resources.GetObject("upButton.Image")));
            this.upButton.Location = new System.Drawing.Point(108, 3);
            this.upButton.Name = "upButton";
            this.upButton.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.upButton.ResourceKey = "Up";
            this.upButton.ResourceModuleKey = "";
            this.upButton.Size = new System.Drawing.Size(99, 25);
            this.upButton.TabIndex = 1;
            this.upButton.Text = "Up";
            this.upButton.Values.ExtraText = "Up";
            this.upButton.Values.Image = ((System.Drawing.Image)(resources.GetObject("upButton.Values.Image")));
            this.upButton.Values.ImageStates.ImageCheckedNormal = null;
            this.upButton.Values.ImageStates.ImageCheckedPressed = null;
            this.upButton.Values.ImageStates.ImageCheckedTracking = null;
            this.upButton.Values.Text = "";
            // 
            // downButton
            // 
            this.downButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.downButton.ButtonStyle = ComponentFactory.Krypton.Toolkit.ButtonStyle.Standalone;
            this.downButton.Image = ((System.Drawing.Image)(resources.GetObject("downButton.Image")));
            this.downButton.Location = new System.Drawing.Point(213, 3);
            this.downButton.Name = "downButton";
            this.downButton.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.downButton.ResourceKey = "Down";
            this.downButton.ResourceModuleKey = "";
            this.downButton.Size = new System.Drawing.Size(99, 25);
            this.downButton.TabIndex = 2;
            this.downButton.Text = "Down";
            this.downButton.Values.ExtraText = "Down";
            this.downButton.Values.Image = ((System.Drawing.Image)(resources.GetObject("downButton.Values.Image")));
            this.downButton.Values.ImageStates.ImageCheckedNormal = null;
            this.downButton.Values.ImageStates.ImageCheckedPressed = null;
            this.downButton.Values.ImageStates.ImageCheckedTracking = null;
            this.downButton.Values.Text = "";
            // 
            // txtName
            // 
            this.txtName.Enabled = false;
            this.txtName.InputControlStyle = ComponentFactory.Krypton.Toolkit.InputControlStyle.Standalone;
            this.txtName.Location = new System.Drawing.Point(120, 3);
            this.txtName.Name = "txtName";
            this.txtName.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.txtName.Size = new System.Drawing.Size(367, 20);
            this.txtName.TabIndex = 17;
            // 
            // lblModule
            // 
            this.lblModule.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblModule.Location = new System.Drawing.Point(3, 31);
            this.lblModule.Name = "lblModule";
            this.lblModule.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblModule.ResourceKey = null;
            this.lblModule.ResourceModuleKey = "Module, TinyERP.Core";
            this.lblModule.Size = new System.Drawing.Size(53, 20);
            this.lblModule.TabIndex = 18;
            this.lblModule.Text = "Module";
            this.lblModule.Values.ExtraText = "";
            this.lblModule.Values.Image = null;
            this.lblModule.Values.Text = "Module";
            // 
            // txtModule
            // 
            this.txtModule.Enabled = false;
            this.txtModule.InputControlStyle = ComponentFactory.Krypton.Toolkit.InputControlStyle.Standalone;
            this.txtModule.Location = new System.Drawing.Point(120, 31);
            this.txtModule.Name = "txtModule";
            this.txtModule.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.txtModule.Size = new System.Drawing.Size(367, 20);
            this.txtModule.TabIndex = 19;
            // 
            // lblName
            // 
            this.lblName.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblName.Location = new System.Drawing.Point(3, 3);
            this.lblName.Name = "lblName";
            this.lblName.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblName.ResourceKey = null;
            this.lblName.ResourceModuleKey = "Name, TinyERP.Core";
            this.lblName.Size = new System.Drawing.Size(43, 20);
            this.lblName.TabIndex = 16;
            this.lblName.Text = "Name";
            this.lblName.Values.ExtraText = "";
            this.lblName.Values.Image = null;
            this.lblName.Values.Text = "Name";
            // 
            // txtDescription
            // 
            this.txtDescription.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtDescription.Enabled = false;
            this.txtDescription.InputControlStyle = ComponentFactory.Krypton.Toolkit.InputControlStyle.Standalone;
            this.txtDescription.Location = new System.Drawing.Point(120, 59);
            this.txtDescription.Multiline = true;
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.txtDescription.Size = new System.Drawing.Size(367, 86);
            this.txtDescription.TabIndex = 24;
            // 
            // chkEnable
            // 
            this.chkEnable.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.chkEnable.Location = new System.Drawing.Point(493, 3);
            this.chkEnable.Name = "chkEnable";
            this.chkEnable.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.chkEnable.ResourceKey = null;
            this.chkEnable.ResourceModuleKey = "Enable, TinyERP.Core";
            this.chkEnable.Size = new System.Drawing.Size(60, 20);
            this.chkEnable.TabIndex = 22;
            this.chkEnable.Text = "Enable";
            this.chkEnable.Values.ExtraText = "";
            this.chkEnable.Values.Image = null;
            this.chkEnable.Values.Text = "Enable";
            // 
            // lblDescription
            // 
            this.lblDescription.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblDescription.Location = new System.Drawing.Point(3, 59);
            this.lblDescription.Name = "lblDescription";
            this.lblDescription.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblDescription.ResourceKey = null;
            this.lblDescription.ResourceModuleKey = "Description, TinyERP.Core";
            this.lblDescription.Size = new System.Drawing.Size(73, 20);
            this.lblDescription.TabIndex = 20;
            this.lblDescription.Text = "Description";
            this.lblDescription.Values.ExtraText = "";
            this.lblDescription.Values.Image = null;
            this.lblDescription.Values.Text = "Description";
            // 
            // gridRoleAccessibility
            // 
            this.gridRoleAccessibility.AllowUserToAddRows = false;
            this.gridRoleAccessibility.AllowUserToDeleteRows = false;
            this.gridRoleAccessibility.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.colGroupName,
            this.colAllowView,
            this.colAllowCreate,
            this.colAllowEdit,
            this.colAllowDelete});
            this.gridRoleAccessibility.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gridRoleAccessibility.GridStyles.Style = ComponentFactory.Krypton.Toolkit.DataGridViewStyle.Mixed;
            this.gridRoleAccessibility.GridStyles.StyleBackground = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.ControlClient;
            this.gridRoleAccessibility.GridStyles.StyleColumn = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            this.gridRoleAccessibility.GridStyles.StyleDataCells = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            this.gridRoleAccessibility.GridStyles.StyleRow = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            this.gridRoleAccessibility.Location = new System.Drawing.Point(0, 26);
            this.gridRoleAccessibility.ModuleAssemblyName = null;
            this.gridRoleAccessibility.Name = "gridRoleAccessibility";
            this.gridRoleAccessibility.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.gridRoleAccessibility.Size = new System.Drawing.Size(585, 255);
            this.gridRoleAccessibility.SortKeyMap = null;
            this.gridRoleAccessibility.SortOnCurrentPage = false;
            this.gridRoleAccessibility.StateCommon.BackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.ControlClient;
            this.gridRoleAccessibility.TabIndex = 14;
            // 
            // colGroupName
            // 
            this.colGroupName.DataPropertyName = "GroupName";
            this.colGroupName.HeaderText = "Role name";
            this.colGroupName.Name = "colGroupName";
            this.colGroupName.ReadOnly = true;
            this.colGroupName.Width = 150;
            // 
            // colAllowView
            // 
            this.colAllowView.DataPropertyName = "AllowView";
            this.colAllowView.HeaderText = "Allow view";
            this.colAllowView.MinimumWidth = 100;
            this.colAllowView.Name = "colAllowView";
            this.colAllowView.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.colAllowView.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.colAllowView.Width = 150;
            // 
            // colAllowCreate
            // 
            this.colAllowCreate.DataPropertyName = "AllowCreate";
            this.colAllowCreate.HeaderText = "Allow create";
            this.colAllowCreate.MinimumWidth = 100;
            this.colAllowCreate.Name = "colAllowCreate";
            this.colAllowCreate.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.colAllowCreate.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.colAllowCreate.Width = 150;
            // 
            // colAllowEdit
            // 
            this.colAllowEdit.DataPropertyName = "AllowEdit";
            this.colAllowEdit.HeaderText = "Allow edit";
            this.colAllowEdit.MinimumWidth = 100;
            this.colAllowEdit.Name = "colAllowEdit";
            this.colAllowEdit.Width = 150;
            // 
            // colAllowDelete
            // 
            this.colAllowDelete.DataPropertyName = "AllowDelete";
            this.colAllowDelete.HeaderText = "Allow delete";
            this.colAllowDelete.MinimumWidth = 100;
            this.colAllowDelete.Name = "colAllowDelete";
            this.colAllowDelete.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.colAllowDelete.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.colAllowDelete.Width = 150;
            // 
            // lblAssignment
            // 
            this.lblAssignment.Dock = System.Windows.Forms.DockStyle.Top;
            this.lblAssignment.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.SuperTip;
            this.lblAssignment.Location = new System.Drawing.Point(0, 0);
            this.lblAssignment.Name = "lblAssignment";
            this.lblAssignment.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblAssignment.ResourceKey = null;
            this.lblAssignment.ResourceModuleKey = "AuthorizationAssignment, TinyERP.Core";
            this.lblAssignment.Size = new System.Drawing.Size(585, 26);
            this.lblAssignment.TabIndex = 23;
            this.lblAssignment.Text = "Authorization assignment";
            this.lblAssignment.Values.ExtraText = "";
            this.lblAssignment.Values.Image = null;
            this.lblAssignment.Values.Text = "Authorization assignment";
            // 
            // FunctionManagementForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.panelMain);
            this.Name = "FunctionManagementForm";
            this.Size = new System.Drawing.Size(810, 522);
            this.Load += new System.EventHandler(this.FunctionManagementForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.panelMain)).EndInit();
            this.panelMain.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitterHorizontal.Panel1)).EndInit();
            this.splitterHorizontal.Panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitterHorizontal.Panel2)).EndInit();
            this.splitterHorizontal.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitterHorizontal)).EndInit();
            this.splitterHorizontal.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.headerGroupFunctionTree.Panel)).EndInit();
            this.headerGroupFunctionTree.Panel.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.headerGroupFunctionTree)).EndInit();
            this.headerGroupFunctionTree.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.headerGroupFunctionDetail.Panel)).EndInit();
            this.headerGroupFunctionDetail.Panel.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.headerGroupFunctionDetail)).EndInit();
            this.headerGroupFunctionDetail.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitterDetail.Panel1)).EndInit();
            this.splitterDetail.Panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitterDetail.Panel2)).EndInit();
            this.splitterDetail.Panel2.ResumeLayout(false);
            this.splitterDetail.Panel2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitterDetail)).EndInit();
            this.splitterDetail.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.panelDetailInfo)).EndInit();
            this.panelDetailInfo.ResumeLayout(false);
            this.tableLayoutPanel.ResumeLayout(false);
            this.tableLayoutPanel.PerformLayout();
            this.flowLayoutPanel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.gridRoleAccessibility)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        
        public FunctionManagementForm()
        {
            InitializeComponent();
            this.gridRoleAccessibility.CellEndEdit += new DataGridViewCellEventHandler(GridRoleAccessibilityCellEndEdit);
            this.saveButton.Click += new EventHandler(SaveButtonClick);
            this.upButton.Click += new EventHandler(UpButtonClick);
            this.downButton.Click += new EventHandler(DownButtonClick);
            this.tvFunction.AfterSelect += new TreeViewEventHandler(TreeFunctionAfterSelect);
            this.saveButton.Enabled = this.AllowEdit;
            this.upButton.Enabled = this.AllowEdit;
            this.downButton.Enabled = this.AllowEdit;
        }
        
        private void DownButtonClick(object sender, EventArgs e)
        {
            if (selectedUINode == null)
            {
                return;
            }
            string entityKey = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Function, TinyERP.Core");
            try
            {
                selectedUINode.Visible = chkEnable.Enabled;
                if (groupACLSortableList != null)
                {
                    foreach (GroupACLDTO groupACL in groupACLSortableList)
                    {
                        GroupACLService.UpdateGroupACL(groupACL);
                    }
                }
                UINodeService.MoveDownNode(selectedUINode);
                ReloadFunctionTree();
                MessageBoxHelper.ShowInformation(CommonMessage.GetSaveSuccessMessage(entityKey));
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(CommonMessage.GetCanNotSaveError(entityKey));
                logger.Error(ex);
            }
        }

        private void UpButtonClick(object sender, EventArgs e)
        {
            if (selectedUINode == null)
            {
                return;
            }
            string entityKey = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Function, TinyERP.Core");
            try
            {
                selectedUINode.Visible = chkEnable.Enabled;
                if (groupACLSortableList != null)
                {
                    foreach (GroupACLDTO groupACL in groupACLSortableList)
                    {
                        GroupACLService.UpdateGroupACL(groupACL);
                    }
                }
                UINodeService.MoveUpNode(selectedUINode);
                ReloadFunctionTree();
                MessageBoxHelper.ShowInformation(CommonMessage.GetSaveSuccessMessage(entityKey));
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(CommonMessage.GetCanNotSaveError(entityKey));
                logger.Error(ex);
            }
        }

        private void TreeFunctionAfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.tvFunction.SelectedNode == null)
            {
                return;
            }
            TreeNode selectedNode = this.tvFunction.SelectedNode;
            selectedUINode = (UINodeDTO)selectedNode.Tag;
            LoadSelectedNode(selectedUINode);
        }

      
        void SaveButtonClick(object sender, EventArgs e)
        {
            SaveData();
        }
        private void SaveData()
        {
            if (selectedUINode == null)
            {
                return;
            }
            string entityKey = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Function, TinyERP.Core");
            try
            {
                selectedUINode.Visible = chkEnable.Enabled;
                if (groupACLSortableList != null)
                {
                    foreach (GroupACLDTO groupACL in groupACLSortableList)
                    {
                        GroupACLService.UpdateGroupACL(groupACL);
                    }
                }
                UINodeService.UpdateUINode(selectedUINode);
                MessageBoxHelper.ShowInformation(CommonMessage.GetSaveSuccessMessage(entityKey));
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(CommonMessage.GetCanNotSaveError(entityKey));
                logger.Error(ex);
            }
        }

        private void GridRoleAccessibilityCellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            gridRoleAccessibility.UpdateCellValue(1, e.RowIndex);
            gridRoleAccessibility.UpdateCellValue(2, e.RowIndex);
            gridRoleAccessibility.UpdateCellValue(3, e.RowIndex);
            gridRoleAccessibility.UpdateCellValue(4, e.RowIndex);
            DataGridViewRow edittingRow = gridRoleAccessibility.Rows[e.RowIndex];
            GroupACLDTO groupACL = (GroupACLDTO)edittingRow.DataBoundItem;
            if (!groupACL.AllowView && e.ColumnIndex == ALLOW_VIEW_COL)
            {
                groupACL.AllowCreate = false;
                groupACL.AllowDelete = false;
                groupACL.AllowEdit = false;
                edittingRow.Cells[ALLOW_CREATE_COL].Value = false;
                edittingRow.Cells[ALLOW_EDIT_COL].Value = false;
                edittingRow.Cells[ALLOW_DELETE_COL].Value = false;
            }
            else if(e.ColumnIndex != ALLOW_VIEW_COL)
            {
                if (groupACL.AllowCreate || groupACL.AllowDelete || groupACL.AllowEdit)
                {
                    groupACL.AllowView = true;
                    edittingRow.Cells[ALLOW_VIEW_COL].Value = true;
                }
            }

            gridRoleAccessibility.Update();
        }
        
        protected void ReloadFunctionTree()
        {
            IList<UINodeDTO> uiNodeList = UINodeService.GetTreeOfUINode();
            tvFunction.Nodes.Clear();
            foreach (UINodeDTO node in uiNodeList)
            {
                tvFunction.Nodes.Add(CreateTreeNode(node));
            }
            tvFunction.ExpandAll();
        }
        protected TreeNode CreateTreeNode(UINodeDTO node)
        {
            TreeNode treeNode = new TreeNode();
            treeNode.Tag = node;
            treeNode.Text = UIManager.GetInstance().GetTitleOfUINode(node);
            if (node.Children != null)
            {
                foreach (UINodeDTO childNode in node.Children)
                {
                     treeNode.Nodes.Add(CreateTreeNode(childNode));
                }
            }
            return treeNode;
        }
        #region ILocalizable Members

        public override void UpdateLanguage()
        {
            this.gridRoleAccessibility.ModuleAssemblyName = "TinyERP.Core";
            base.UpdateLanguage();
            headerGroupFunctionDetail.ValuesPrimary.Heading = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("FunctionDetail, TinyERP.Core");
            headerGroupFunctionTree.ValuesPrimary.Heading = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("FunctionMenu, TinyERP.Core");
        }

        #endregion

        private void FunctionManagementForm_Load(object sender, EventArgs e)
        {
            ReloadFunctionTree();
        }

       
        private void LoadSelectedNode(UINodeDTO node)
        {
            txtName.Text = UIManager.GetInstance().GetTitleOfUINode(node);
            txtDescription.Text = node.Description;
            txtModule.Text = node.ModuleName;
            chkEnable.Checked = node.Visible;
            //TODO: load authorization list
            
            IList groupACLList = GroupACLService.GetAllACLOfUINode(node.ID);
            groupACLSortableList = SortableBindingList<GroupACLDTO>.CreateSortableBindingList(groupACLList);
            this.gridRoleAccessibility.DataSource = groupACLSortableList;
            this.gridRoleAccessibility.Refresh();
        }

       

        
      
    }
}
